﻿using System;
using System.Windows.Forms;
using Microsoft.LiveFX.Client;
using Google.GData.Contacts;
using System.Net;
using Microsoft.LiveFX.ResourceModel;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmLiveGoogleLogin : FrmDefault
    {
        public FrmLiveGoogleLogin()
        {
            InitializeComponent();
            TypeLogin =  PhoneStorageEnum.Live;
            User = "";
            Pwd = "";
        }
        public string User { get; set; }
        public string Pwd { get; set; }
        public bool SavePwd { get; set; }
        public bool SaveUser { get; set; }
        public PhoneStorageEnum TypeLogin { get; set; }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmLiveGoogleLogin_Load(object sender, EventArgs e)
        {
            TxtUser.Text = this.User;
            TxtPwd.Text = this.Pwd;
            ChkSaveLogin.Checked = this.SaveUser;
            ChkSavePwd.Checked = this.SavePwd;
            bool ok = false;
            if (TypeLogin == PhoneStorageEnum.Live)
            {
                LblTitSync.Text = "Live";
                PctImage.Image = Properties.Resources.live;
                ok = !SharedLib.LiveIsLogin;
            }
            else if (TypeLogin == PhoneStorageEnum.Goolgle)
            {
                LblTitSync.Text = "Google";
                PctImage.Image = Properties.Resources.GOOGLE;
                ok = !SharedLib.GoogleIsLogin;
            }
            else
            {
                this.Close();
            }
            if (!ok)
            {
                TxtUser.Enabled = false;
                TxtPwd.Enabled = false;
                BntLogin.Text = ResourcesText.Logout;
            }
            else
            {
                TxtUser.Enabled = true;
                TxtPwd.Enabled = true;
                BntLogin.Text = ResourcesText.Login;
            }
        }

        private void BntLogin_Click(object sender, EventArgs e)
        {
            if (TypeLogin == PhoneStorageEnum.Live)
            {
                LblErro.Visible = false;
                SharedLib.LiveOpeEnv = new LiveOperatingEnvironment();
                if (SharedLib.LiveIsLogin)
                {
                    SharedLib.LiveIsLogin = false;
                    BntLogin.Text = ResourcesText.Login;
                    this.TxtUser.Enabled = true;
                    TxtPwd.Enabled = true;
                    Properties.Settings.Default.DefaultLiveUser = ChkSaveLogin.Checked?TxtUser.Text:"";
                    Properties.Settings.Default.DefaultLivePwd = ChkSavePwd.Checked?SharedLib.Encrypt(TxtPwd.Text):"";
                    Properties.Settings.Default.DefaultLiveSaveUser = ChkSaveLogin.Checked;
                    Properties.Settings.Default.DefaultLiveSavePwd = ChkSavePwd.Checked;
                    SharedLib.SaveSettings();
                    this.Close();
                    return;
                }
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    NetworkCredential credentials = new NetworkCredential(TxtUser.Text, TxtPwd.Text);
                    SharedLib.LiveOpeEnv.Connect(credentials, new LiveItemAccessOptions());
                    SharedLib.LiveIsLogin = true;
                    SharedLib.LiveUser = TxtUser.Text;
                    SharedLib.LivePwd = TxtPwd.Text;
                }
                catch (Exception ex)
                {
                    if (ex.Message.ToLower().Contains("(404) not found"))
                    {
                        bool ok = SharedLib.MsgQuestion(ResourcesText.NotAzureAccount);
                        if (ok)
                        {
                            System.Diagnostics.Process.Start("https://lx.azure.microsoft.com/Cloud/Launch/Redeem.aspx?Token=FFFFF-FFFFF-FFFFF-FFFFF-FFFFF");
                        }
                    }
                    else
                    {
                        SharedLib.MsgErro(ex.Message);
                    }
                    SharedLib.LiveIsLogin = false;
                    SharedLib.LiveUser = "";
                    SharedLib.LivePwd = "";
                    LblErro.Visible = true;
                }
            }
            else if (TypeLogin == PhoneStorageEnum.Goolgle)
            {
                SharedLib.Googleservice = new ContactsService(SharedLib.AssemblyProduct);
                if (SharedLib.GoogleIsLogin)
                {
                    SharedLib.GoogleIsLogin = false;
                    BntLogin.Text = ResourcesText.Login;
                    this.TxtUser.Enabled = true;
                    Properties.Settings.Default.DefaultGoogleUser = ChkSaveLogin.Checked ? TxtUser.Text : "";
                    Properties.Settings.Default.DefaultGooglePwd = ChkSavePwd.Checked ? SharedLib.Encrypt(TxtPwd.Text) : "";
                    Properties.Settings.Default.DefaultGoogleSaveUser = ChkSaveLogin.Checked;
                    Properties.Settings.Default.DefaultGoogleSavePwd = ChkSavePwd.Checked;
                    SharedLib.GoogleUser = Properties.Settings.Default.DefaultGoogleUser;
                    SharedLib.GooglePwd = Properties.Settings.Default.DefaultGooglePwd;
                    SharedLib.SaveSettings();
                    TxtPwd.Enabled = true;
                    this.Close();
                    return;
                }
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    SharedLib.Googleservice.setUserCredentials(TxtUser.Text, TxtPwd.Text);
                    ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
                    query.NumberToRetrieve = 1;
                    SharedLib.Googleservice.Query(query);
                    SharedLib.GoogleIsLogin = true;
                    SharedLib.GoogleUser = TxtUser.Text;
                    SharedLib.GooglePwd = TxtPwd.Text;
                }
                catch (Exception ex)
                {
                    SharedLib.MsgErro(ex.Message);
                    SharedLib.GoogleIsLogin = false;
                    SharedLib.GoogleUser = "";
                    SharedLib.GooglePwd = "";
                    LblErro.Visible = true;
                }
            }
            if ((TypeLogin == PhoneStorageEnum.Live && SharedLib.LiveIsLogin) || (TypeLogin == PhoneStorageEnum.Goolgle && SharedLib.GoogleIsLogin))
            {
                string usu = "";
                if (ChkSaveLogin.Checked)
                {
                    usu = TxtUser.Text;
                }
                string pwd = "";
                if (ChkSavePwd.Checked)
                {
                    pwd = SharedLib.Encrypt(TxtPwd.Text);
                }
                if (TypeLogin == PhoneStorageEnum.Live)
                {
                    Properties.Settings.Default.DefaultLiveUser = usu;
                    Properties.Settings.Default.DefaultLivePwd = pwd;
                    Properties.Settings.Default.DefaultLiveSaveUser = ChkSaveLogin.Checked;
                    Properties.Settings.Default.DefaultLiveSavePwd = ChkSavePwd.Checked;
                }
                else if (TypeLogin == PhoneStorageEnum.Goolgle)
                {
                    Properties.Settings.Default.DefaultGoogleUser = usu;
                    Properties.Settings.Default.DefaultGooglePwd = pwd;
                    Properties.Settings.Default.DefaultGoogleSaveUser = ChkSaveLogin.Checked;
                    Properties.Settings.Default.DefaultGoogleSavePwd = ChkSavePwd.Checked;
                }
                SharedLib.SaveSettings();
            }
            this.Cursor = Cursors.Default;
            if (TypeLogin == PhoneStorageEnum.Live && SharedLib.LiveIsLogin)
            {
                this.Close();
            }
            if (TypeLogin == PhoneStorageEnum.Goolgle && SharedLib.GoogleIsLogin)
            {
                this.Close();
            }
            this.Cursor = Cursors.Default;
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void ChkSavePwd_CheckedChanged(object sender, EventArgs e)
        {
            if (TypeLogin == PhoneStorageEnum.Live)
            {
                Properties.Settings.Default.DefaultLiveSavePwd = ChkSavePwd.Checked;
                if (!ChkSavePwd.Checked)
                {
                    Properties.Settings.Default.DefaultLivePwd = "";
                }
                else
                {
                    string pwd = SharedLib.Encrypt(TxtPwd.Text);
                    Properties.Settings.Default.DefaultLivePwd = pwd;
                }
            }
            else if (TypeLogin == PhoneStorageEnum.Goolgle)
            {
                Properties.Settings.Default.DefaultGoogleSavePwd = ChkSavePwd.Checked;
                if (!ChkSavePwd.Checked)
                {
                    Properties.Settings.Default.DefaultGooglePwd = "";
                }
                else
                {
                    string pwd = SharedLib.Encrypt(TxtPwd.Text);
                    Properties.Settings.Default.DefaultGooglePwd = pwd;
                }
            }
            SharedLib.SaveSettings();
        }

        private void ChkSaveLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (TypeLogin == PhoneStorageEnum.Live)
            {
                Properties.Settings.Default.DefaultLiveSaveUser = ChkSaveLogin.Checked;
                if (!ChkSaveLogin.Checked)
                {
                    Properties.Settings.Default.DefaultLiveUser = "";
                }
                else
                {
                    Properties.Settings.Default.DefaultLiveUser = TxtUser.Text;
                }
            }
            else if (TypeLogin == PhoneStorageEnum.Goolgle)
            {
                Properties.Settings.Default.DefaultGoogleSaveUser = ChkSaveLogin.Checked;
                if (!ChkSaveLogin.Checked)
                {
                    Properties.Settings.Default.DefaultGoogleUser = "";
                }
                else
                {
                    Properties.Settings.Default.DefaultGoogleUser = TxtUser.Text;
                }
            }
            SharedLib.SaveSettings();
        }
    }
}
